<template>
	<view class="popupModel">
		<uni-popup ref="popup" type="bottom" safe-area background-color="#fff" @maskClick='details.close'>
			<image class="colse" @tap="details.close" :src="url+'icon-188.png'" mode=""></image>
			<view class="purchase" @touchmove.stop.prevent>
				<view class="address" v-if="details.goodsInfo.address">
					<view class="content_title">
						<image :src="url+'icon-92.png'" mode=""></image>
						<text class="xjs-line-one">{{details.goodsInfo.address.name?details.goodsInfo.address.name:''}} {{ details.goodsInfo.address.province }}{{ "-" + details.goodsInfo.address.city}}{{ details.goodsInfo.address.area ? "-" + details.goodsInfo.address.area : "" }}</text>
						<!-- <uni-icons type="right" :color="'#707272'" :size="16"></uni-icons> -->
					</view>
					<view class="content_peisong">{{details.goodsInfo.preSale?'现货':'预售'}}，{{details.goodsInfo.spotCustDeliveryMsg}}</view>
				</view>
				<view class="shop">
					<image class="shop_avater" :src="details.thumPic" mode=""></image>
					<view>
						<view class="name xjs-line-one">{{details.goodsInfo.name}}</view>
						<view class="price">¥{{details.totalPrice}}</view>
						<view class="num">
							<view class="num_content">
								<view class="view" @tap="details.landnumreduce" :class="{ inputdisable: details.inputValue == 1 }">-</view>
								<input class="view" type="number" @input="details.inputValueClick" v-model="details.inputValue" @blur="details.onBlur" />
								<view class="view" @tap="details.landnumadd"
							:class="{inputdisable: details.inputValue == details.repertoryAmount}">+</view>
							</view>
							<view class="xian"><text v-if="details.goodsInfo.purchaseLimitType != 0"></text> <text class="cun">库存{{details.repertoryAmount}}</text></view>
						</view>
					</view>
				</view>
				<view class="detailsSelect" v-if="details.goodsInfo.goodsSkuStockList && details.goodsInfo.ifSkus == 1">
					<scroll-view :scroll-top="0" scroll-y="true" class="scroll-Y">
						<view class="speSel_item" v-for="(item, index) in details.data[details.specListName]" :key="index">
							<view class="speSel_item_title">{{ item.name }}</view>
							<view class="speSel_item_view">
								<view
									:class="[itemTwo.ishow ? '' : 'outOfStock', details.subIndex[index] == indexTwo ? 'active' : '']"
									v-for="(itemTwo, indexTwo) in item.list"
									:key="indexTwo"
									@click="details.skuClick(itemTwo, index, indexTwo)">
									{{ itemTwo.name }}
									<text class="outOfStockText" v-if="itemTwo.outOfStock && !itemTwo.ishow">缺货</text>
									<text class="outOfStockText" v-if="itemTwo.isPreSale && itemTwo.ishow">预售</text>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
				<view v-if="details.goodsInfo.numberAdd == 3" class="buttonTwo">
					<view @tap="details.btnthrottle(1)">加入购物车</view>
					<view @tap="details.btnthrottle(2)">立即购买</view>
				</view>
				<view v-else class="button" @click="details.btnthrottle"><view>确定</view></view>
			</view>
		</uni-popup>
	</view>
</template>
<script lang="ts" setup>
import { reactive, watch,ref,defineEmits,nextTick } from 'vue';
const url = ref<string>('https://xjs-oss-prod.oss-cn-shenzhen.aliyuncs.com/xjs-static/')
const props = defineProps(['showPop','cartMessage'])
import { useCartStoreToRefs,useCartStore } from "@/store/useCartStore";
import { useOrderStoreToRefs } from "@/store/useOrderStore"
const { addressInfo } = useOrderStoreToRefs()
const {brandOrderList,carLen} = useCartStoreToRefs();
const { addCart } = useCartStore()
const emit = defineEmits(['open','selectShop']);
const popup = ref(null)
watch(()=>props.showPop,(val)=>{
	if(val){
		details.open();
		details.flay = true
		popup.value.open('bottom')
	}else{
		popup.value.close()
	}
})

const details = reactive<any>({
	goodsInfo: {}, //商品详情数据
	inputValue: 1, //购买数量
	repertoryAmount: 200, // 库存
	oneRepertoryAmount: 1, //一元购可购买数量
	checkedLetter: true, //是否刻字
	purchaseLimit: {}, //限购信息
	chooseLetter: false, //刻字弹框
	letterdata: "", //刻字内容
	stockNum: 0, //已选规格的库存
	
	data: {}, //数据集
	selectArr: [], // 存放被选中的值
	subIndex: [], // 是否选中 因为不确定是多规格还是单规格，所以这里定义数组来判断
	selectShop: {}, // 存放最后选中的商品
	selectNum: 1, // 最小购买数量 选中数量
	outFoStock: false, // 是否全部sku都缺货
	specListName: "spec_list", //需要筛选的参数
	skuListName: "sku_list", //需要筛选的参数
	complete: false, // 组件是否加载完成
	initKey: true, // 是否已初始化
	stockName: "stock", //库存的字段名
	skuArrName: "sku_name_arr", //sku组合路径的字段名
	maxBuyNum: 200, // 最大购买数量
	minBuyNum: 1, //最小购买数量
	selectShopStr: "", //已选规格
	totalPrice: "", //价格
	thumPic: "", //规格图片
	spotDeliveryMsg: {
		//几点发货
		str: "",
		num: 0
	},
	isUrlTo: true, //判断是否可以刻字完成直接进行下一步
	preSale: true, //true  现货  false  预售
	originalPrice: null, //原价
	sgMsg: {},
	openModel: false,
	integralShow: false, //积分不足的弹框
	costPrice: "", //原价
	flay:true,
	numThress: 1, //在numberAdd==3的情况下，点击了哪个
	seeScroll: 0, //规格滚动距离
	close:(value?:Boolean)=>{//关闭页面
		emit('selectShop',details.selectShop,value)
	},
	btnthrottle:(num?)=>{
		details.numThress = num
		// numberAdd  1  加入购物车  2  立即购买  3  选择规格颜色  return信息  4  购物车进入 回显 替换 
		// if (details.goodsInfo.numberAdd == 3) {
		// 	details.close();
		// 	return;
		// }
		if (!details.selectShop.selectShopStr && details.goodsInfo.ifSkus == 1) {
			uni.showToast({
				title: '请选择规格',
				icon: 'none'
			})
			return;
		}
		if (details.goodsInfo.numberAdd == 1 || details.goodsInfo.numberAdd == 3&&details.numThress==1 || details.goodsInfo.numberAdd == 4) {
			details.addShopCart(); //加入购物车
		} else {
			details.buy();
		}
	},
	// 加入购物车
	addShopCart:async()=> {
		if (details.flay) {
			details.flay = false;
			let goodsInfo = details.goodsInfo;
			goodsInfo.amount = details.inputValue;
			goodsInfo.prepareStocks = details.selectShop.prepareStocks;
			goodsInfo.stocks = details.selectShop.stocks;
			goodsInfo.removeSkuCode = goodsInfo.skuCode;
			goodsInfo.skuCode = details.goodsInfo.ifSkus == 1 ? details.selectShop.id : details.goodsInfo.goodsSkuStockList[0].id;
			goodsInfo.goodsSkuStockList.forEach((item) => {
				if (item.id == goodsInfo.skuCode) {
					goodsInfo.skuProperties = item.properties;
				}
			});
			const params = {
				addType: 1,
				arr: [goodsInfo],
				ifRadio: false,
				isWaiting: true,
				arrType: 1,
				isSelect:true
			};
			if (details.goodsInfo.numberAdd == 4) {
				params.addType = 6;
				params.isSelect = true;
			}
			const res:any = await addCart(params);
			if (res.code == 0) {
				if (details.goodsInfo.numberAdd == 4) {
				}else{
					uni.showToast({
						title: '加入购物车成功！',
						icon: 'none'
					})
				}
				
				details.close();
			}
			details.flay = true;
		}
	},
	// 立即购买
	buy:async()=> {
		if (details.inputValue > 0) {
			if (details.flay) {
				details.flay = false;
				if (details.goodsInfo.status == 2) {
					//在售状态 0-即将开售，1-在售 2-下架
					//已加入购物车的商品下架后进入商品详情进行购买
					uni.showToast({
						title: '该商品已下架,看看其他的吧!',
						icon: 'none'
					})
					details.flay = true;
				} else if (details.goodsInfo.goodsStatus == 3 || (details.goodsInfo.goodsStatus == 2 && details.goodsInfo.saleType == 1)) {
					//缺货
					uni.showToast({
						title: '该商品已售罄,看看其他的吧!',
						icon: 'none'
					})
					details.flay = true;
				} else {
					let goodsInfo = details.goodsInfo;
					goodsInfo.amount = details.inputValue;
					goodsInfo.count = goodsInfo.amount;
					goodsInfo.goodsId = goodsInfo.id;
					// goodsInfo.refereeId = details.$store.state.user.rebrandId;
	
					goodsInfo.prepareStocks = details.selectShop.prepareStocks;
					goodsInfo.stocks = details.selectShop.stocks;
					goodsInfo.price = details.totalPrice;
					goodsInfo.thumPic = details.thumPic;
					goodsInfo.deliverWeight = details.selectShop.weight;
					goodsInfo.deliverVolume = details.selectShop.volume; //发货体积
					goodsInfo.skuCode = details.goodsInfo.ifSkus == 1 ? details.selectShop.id : details.goodsInfo.goodsSkuStockList[0].id; //发货重量
					goodsInfo.goodsSkuStockList.forEach((item) => {
						if (item.id == goodsInfo.skuCode) {
							goodsInfo.skuProperties = item.properties;
						}
					});
					if (details.goodsInfo.integral) {
						// 1  现金购买   2  原价购买   3  积分兑换
						goodsInfo.scoreType = "score";
						goodsInfo.buyCostPrice = 1;
						// 原价购买
						if (details.goodsInfo.integral == 1 || details.goodsInfo.integral == 2) {
							goodsInfo.price = details.totalPrice;
						}
					}
					brandOrderList.value = [goodsInfo]
					details.close(); //隐藏弹框
					uni.navigateTo({
						url: `/pages/shopping/confirmOrder/index?productType=product&prohibit=true`
					});
					details.inputValue = 1; //恢复数量输入框值为1
					details.flay = true;
				}
			}
		} else {
			uni.showToast({
				title: '购买数量不能为空',
				icon: 'none'
			})
		}
	},
	open:()=> {
		details.goodsInfo = props.cartMessage;
		console.log(details.goodsInfo)
		if (details.goodsInfo) {
			// if(JSON.stringify(this.data) != "{}") return
			if (details.goodsInfo.ifSkus == 1) {
				var lists = [],
					data:any = {};
				data.sku_list = details.goodsInfo.goodsSkuStockList;
				data.sku_list.forEach((item) => {
					item.sku_name_arr = [];
					var propertiesList = JSON.parse(item.properties);
					propertiesList.forEach((i) => {
						item.sku_name_arr.push(i.value);
					});
				});
				details.goodsInfo.goodsSkuStockList.forEach((item) => {
					item.stock = item.stocks + item.prepareStocks;
					item.propertiesList = JSON.parse(item.properties);
					item.propertiesList.forEach((i, index) => {
						lists.push(i);
					});
				});
				let newobj = {};
				lists = lists.reduce((preVal, curVal) => {
					newobj[curVal.value] ? "" : (newobj[curVal.value] = preVal.push(curVal));
					return preVal;
				}, []);
				let arrList = [];
				lists.forEach((item, index) => {
					if (arrList.length <= 0) {
						arrList.push({
							name: item.key,
							list: [
								{
									name: item.value
								}
							]
						});
					} else {
						let res = arrList.find((citem, i) => {
							if (citem.name == item.key || citem.name == item.key) {
								arrList[i].list.push({
									name: item.value
								});
								return true;
							}
						});
						if (!res) {
							arrList.push({
								name: item.key,
								list: [
									{
										name: item.value
									}
								]
							});
						}
					}
				});
				data.spec_list = arrList;
				details.updateGoodsInfo(data); // 更新商品信息(库存、名称、图片)
				emit('open',true)
				let item = details.goodsInfo.goodsSkuStockList.find((item) => {
					return item.stocks + item.prepareStocks > 0;
				});
				details.thumPic = item.pic;
				details.totalPrice = item.price
			} else {
				details.selectShop = details.goodsInfo.goodsSkuStockList[0];
				details.preSale = details.goodsInfo.goodsSkuStockList[0].stocks != 0 ? true : false;
				details.repertoryAmount = details.goodsInfo.goodsSkuStockList[0].stocks + details.goodsInfo.goodsSkuStockList[0].prepareStocks;
				details.totalPrice = details.goodsInfo.goodsSkuStockList[0].price
				// details.getGoodsDetailPrice(details.goodsInfo.goodsSkuStockList[0].id);
			}
		}
		if (details.goodsInfo.numberAdd == 4) {
			details.inputValue = details.goodsInfo.amount;
		}
		if (details.goodsInfo.spotDeliveryMsg) {
			let spotDeliveryMsg = details.goodsInfo.spotDeliveryMsg.split(",");
			details.spotDeliveryMsg.str = spotDeliveryMsg[0];
			if (spotDeliveryMsg.length == 2) {
				details.spotDeliveryMsg.num = spotDeliveryMsg[1];
			}
		}
		details.purchaseLimit = {};
		//判断限购显示信息 qc
		if (details.goodsInfo.purchaseLimitType != 0 && !details.goodsInfo.oneBuyPromotionVO) {
			if (details.goodsInfo.purchaseLimitType == 1) {
				var arr = details.goodsInfo.purchaseLimitRules.split(",");
			}
			var num = details.goodsInfo.purchaseLimitType == "1" ? arr[1] : details.goodsInfo.purchaseLimitRules;
			if (details.goodsInfo.purchaseLimitType == 1) {
				var day = arr[0] < 7 ? "每天" : arr[0] == 7 ? "每周" : arr[0] <= 31 ? "每月" : "每年";
			} else {
				var day = "永久";
			}
			details.purchaseLimit.num = num;
			details.purchaseLimit.day = day;
			if (num < details.inputValue) {
				details.inputValue = num;
			}
		}
		// 自定义说明
		if (!details.goodsInfo.spotCustDeliveryMsg || details.goodsInfo.spotCustDeliveryMsg == "") {
			details.goodsInfo.spotCustDeliveryMsg = `${details.goodsInfo.spotDeliveryMsg ? details.spotDeliveryMsg.str : 15}点前下单当天发货，${
				details.goodsInfo.spotDeliveryMsg ? details.spotDeliveryMsg.str : 15
			}点后的订单次日发货${details.goodsInfo.spotDeliveryMsg && details.spotDeliveryMsg.num == 1 ? ",周末及法定节假日顺延" : ""}`;
		}
	},
	//购买数量减
	landnumreduce:()=> {
		if (details.inputValue > 1) {
			details.inputValue--;
		} else {
			uni.showToast({
				title: '最低购买一件！',
				icon: 'none'
			})
			return false;
		}
	},
	//购买数量加
	landnumadd() {
		let repertoryAmount = details.repertoryAmount;
		if (details.inputValue < repertoryAmount) {
			details.inputValue++;
		} else {
			uni.showToast({
				title: `最多只能购买${repertoryAmount}件`,
				icon: 'none'
			})
			return false;
		}
		//如果超出限购数量 提示  数量为限购数量
		if (details.goodsInfo.purchaseLimitType != 0 && details.purchaseLimit.num - details.goodsInfo.alreadyBuyNum < details.inputValue) {
			details.inputValue = details.purchaseLimit.num - details.goodsInfo.alreadyBuyNum <= 0 ? 1 : details.purchaseLimit.num - details.goodsInfo.alreadyBuyNum;
			uni.showToast({
				title: `最多可购买${details.purchaseLimit.num - details.goodsInfo.alreadyBuyNum < 0 ? 0 : details.purchaseLimit.num - details.goodsInfo.alreadyBuyNum}件`,
				icon: 'none'
			})
		}
	},
	//购买数量  填写
	inputValueClick:(e)=> {
		nextTick(() => {
			setTimeout((_) => {
				// 校验输入的数量是不是正整数
				if (details.inputValue === "" && details.inputValue != 0) {
					uni.showToast({
						title: '件数只能为正整数哦~',
						icon: 'none'
					})
					details.inputValue = 1;
				}
				if (String(details.inputValue).indexOf(".") > -1) {
					uni.showToast({
						title: '件数只能为整数',
						icon: 'none'
					})
					details.inputValue = parseInt(details.inputValue);
				}
				//如果 输入值大于库存,输入值就等于库存
				if (details.inputValue > details.repertoryAmount) {
					details.inputValue = details.repertoryAmount;
					uni.showToast({
						title: "最多只能购买" + details.repertoryAmount + "件",
						icon: 'none'
					})
				}
				// 针对app里面uni的input赋值不成功的处理111  e.detail.value> this.maxuserbagnum
				if (e.detail.value > details.repertoryAmount) {
					details.inputValue = details.repertoryAmount;
					uni.showToast({
						title: "最多只能购买" + details.repertoryAmount + "件",
						icon: 'none'
					})
				}
				//如果超出限购数量 提示  数量为限购数量
				if (details.purchaseLimit && details.purchaseLimit.num - details.goodsInfo.alreadyBuyNum < details.inputValue) {
					var num = details.purchaseLimit.num - details.goodsInfo.alreadyBuyNum < 0 ? 0 : details.purchaseLimit.num - details.goodsInfo.alreadyBuyNum;
					if (num == 0) {
						uni.showToast({
							title: `已达到限购件数，不能购买`,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: `最多可购买${num}件`,
							icon: 'none'
						})
					}
	
					details.inputValue = num;
				}
			}, 0);
		});
	},
	
	// 输入框获取焦点
	onBlur:(event)=> {
		let value = event.detail.value;
		if (!value) {
			details.inputValue = 1;
			return;
		}
		value = +value;
		let num = details.selectShop.stock;
		if (value > num) {
			value = num;
		} else if (value < 1) {
			value = 1;
		}
		details.inputValue = value;
	},
	// sku初始化
	init:() => {
		// 清空之前的数据
		details.selectArr = []
		details.subIndex = [];
		details.selectShop = {};
		details.selectNum = 1;
		details.outFoStock = false;
		details.shopItemInfo = {};
		let specListName = details.specListName;
		// 处理显示数据
		details.data[specListName].map((item) => {
			details.selectArr.push("");
			details.subIndex.push(-1);
		});
		details.checkItem(); // 计算sku里面规格形成路径
		details.checkInpath(-1); // 传-1是为了不跳过循环
		// if (!notAutoClick) that.autoClickSku(); // 自动选择sku策略
	},
	// 更新商品信息(库存、名称、图片)
	updateGoodsInfo:(data)=> {
		let { skuListName } = details
		details.data = data;
		details.initKey = true;
		if (details.initKey) {
			details.initKey = false;
			details.init();
		}
		// 更新选中sku的库存信息
		let select_sku_info = details.getListItem(details.data[skuListName], details.skuIdName, details.selectShop[details.skuIdName]);
		Object.assign(details.selectShop, select_sku_info);
		details.defaultSelectSku();
		details.complete = true;
	},
	defaultSelectSku:()=> {
		let { defaultSelect } = details;
		if (defaultSelect && defaultSelect.sku && defaultSelect.sku.length > 0) {
			details.selectSku(defaultSelect);
		}
	},
	selectSku:(obj:any = {})=> {
		let { sku: skuArr, num: selectNum } = obj;
		let specListArr = details.data[details.specListName];
		if (skuArr && specListArr.length === skuArr.length) {
			// 先清空
			let skuClickArr = [];
			let clickKey = true;
			for (let index = 0; index < skuArr.length; index++) {
				let skuName = skuArr[index];
				let specList = specListArr[index].list;
				let index1 = index;
				let index2 = details.getListIndex(specList, "name", skuName);
				if (index2 == -1) {
					clickKey = false;
					break;
				}
				skuClickArr.push({
					spec: specList[index2],
					index1: index1,
					index2: index2
				});
			}
			if (clickKey) {
				details.init()
				skuClickArr.map((item) => {
					details.skuClick(item.spec, item.index1, item.index2, true);
				});
			}
		}
		if (selectNum > 0) details.selectNum = selectNum;
	},
	priceFilter:(n = 0)=> {
		if (typeof n == "string") {
			n = parseFloat(n);
		}
		return (n / 100).toFixed(2);
	},
	// 计算sku里面规格形成路径
	checkItem:()=> {
		let that = this;
		let { stockName } = details;
	
		let skuListName = details.skuListName;
		// 去除库存小于等于0的商品sku
		let originalSkuList = details.data[skuListName];
		let skuList = [];
		let stockNum = 0;
		originalSkuList.map((skuItem, index) => {
			// skuList.push(skuItem);
			// stockNum += skuItem[stockName];
			if (skuItem[stockName] >= 0) {
				skuList.push(skuItem);
				stockNum += skuItem[stockName];
			}
		});
		if (stockNum <= 0) {
			details.outFoStock = true;
		}
		// 计算有多小种可选路径
		let result = skuList.reduce(
			(arrs, items) => {
				return arrs.concat(
					items[details.skuArrName].reduce(
						(arr, item) => {
							return arr.concat(
								arr.map((item2) => {
									// 利用对象属性的唯一性实现二维数组去重
									if (!details.shopItemInfo.hasOwnProperty(details.getArrayToSting([...item2, item]))) {
										details.shopItemInfo[details.getArrayToSting([...item2, item])] = items;
									}
									return [...item2, item];
								})
							);
						},
						[[]]
					)
				);
			},
			[[]]
		);
	},
	//拼接
	getArrayToSting:(arr)=> {
		let str = "";
		arr.map((item, index) => {
			item = item.replace(/\./g, "。");
			if (index == 0) {
				str += item;
			} else {
				str += "," + item;
			}
		});
		return str;
	},
	// 自动选择sku前提是只有一组sku,默认自动选择最前面的有库存的sku
	autoClickSku:()=> {
		let that = this;
		let skuList = details.data[details.skuListName];
		let specListArr = details.data[details.specListName];
		if (specListArr.length == 1) {
			let specList = specListArr[0].list;
			for (let i = 0; i < specList.length; i++) {
				let sku = details.getListItem(skuList, details.skuArrName, [specList[i].name]);
				if (sku) {
					details.skuClick(specList[i], 0, i);
					break;
				}
			}
		}
	},
	// 检查路径 循环所有属性判断哪些属性可选  当前选中的兄弟节点和已选中属性不需要循环
	checkInpath:(clickIndex)=> {
		let specListName = details.specListName;
		//循环所有属性判断哪些属性可选
		//当前选中的兄弟节点和已选中属性不需要循环
		let specList = details.data[specListName];
		for (let i = 0, len = specList.length; i < len; i++) {
			if (i == clickIndex) {
				continue;
			}
			let len2 = specList[i].list.length;
			for (let j = 0; j < len2; j++) {
				// if (that.subIndex[i] != -1 && j == that.subIndex[i]) {
				// 	continue;
				// }
				let choosed_copy = [...details.selectArr];
				choosed_copy[i] = specList[i].list[j].name
				// that.$set(choosed_copy, i, ;
				let choosed_copy2 = choosed_copy.filter((item) => item !== "" && typeof item !== "undefined");
	
				let key = details.getArrayToSting(choosed_copy2);
				if (details.shopItemInfo.hasOwnProperty(key) && details.shopItemInfo[key].stock != 0) {
					specList[i].list[j].ishow = true;
					// 判断预售
					if (details.shopItemInfo[key].stocks == 0 && details.shopItemInfo[key].stocksprepareStocks != 0) {
						specList[i].list[j].isPreSale = true;
					} else {
						specList[i].list[j].isPreSale = false;
					}
				} else {
					specList[i].list[j].ishow = false;
					// 判断是否没有货了 缺货
					if (details.shopItemInfo.hasOwnProperty(key) && Number(details.shopItemInfo[key].stock) == 0) {
						specList[i].list[j].outOfStock = true;
					} else {
						specList[i].list[j].outOfStock = false;
					}
				}
			}
		}
		details.data[specListName] = specList
		// details.$set(that.data, specListName, specList);
	},
	// 获取对象数组中的某一个item,根据指定的键值
	getListItem:(list, key, value)=> {
		let that = this;
		let item;
		for (let i in list) {
			if (typeof value == "object") {
				if (JSON.stringify(list[i][key]) === JSON.stringify(value)) {
					item = list[i];
					break;
				}
			} else {
				if (list[i][key] === value) {
					item = list[i];
					break;
				}
			}
		}
		return item;
	},
	getListIndex:(list, key, value)=> {
		let that = this;
		let index = -1;
		for (let i = 0; i < list.length; i++) {
			if (list[i][key] === value) {
				index = i;
				break;
			}
		}
		return index;
	},
	// 检测是否已经选完sku
	checkSelectShop:()=> {
		// 如果全部选完
		if (details.selectArr.every((item) => item != "")) {
			details.selectShopStr = "";
			details.selectShop = details.shopItemInfo[details.getArrayToSting(details.selectArr)];
			details.repertoryAmount = details.selectShop.stock ? details.selectShop.stock : 200;
			details.preSale = details.selectShop.stocks > 0 ? true : false;
			if (details.inputValue > details.repertoryAmount) {
				details.inputValue = details.repertoryAmount;
			}
			let stocks = details.selectShop[details.stockName];
			if (typeof stocks !== "undefined" && details.selectNum > stocks) {
				details.selectNum = stocks;
			}
			if (details.selectNum > details.maxBuyNum) {
				details.selectNum = details.maxBuyNum;
			}
			if (details.selectNum < details.minBuyNum) {
				details.selectNum = details.minBuyNum;
			}
			if (details.selectedInit) {
				details.selectNum = details.minBuyNum || 1;
			}
		} else {
			details.selectShop = {};
		}
		if (details.selectShop.sku_name_arr) {
			details.selectShop.sku_name_arr.forEach((item) => {
				details.selectShopStr += item + " ; ";
			});
			details.thumPic = details.selectShop.pic;
			details.selectShopStr = details.selectShopStr.substr(0, details.selectShopStr.length - 2);
			details.selectShop.selectShopStr = details.selectShopStr;
			details.totalPrice = details.selectShop.price
			// details.getGoodsDetailPrice();
		} else {
			details.selectShopStr = "";
			details.selectShop.selectShopStr = "";
		}
	},
	// sku按钮的点击事件
	skuClick:(value, index1, index2, offer?)=> {
		if (value.ishow || offer) {
			if (details.selectArr[index1] != value.name) {
				details.selectArr[index1] = value.name
				details.subIndex[index1] = index2
			} else {
				details.selectArr[index1] = ''
				details.subIndex[index1] = -1
			}
			details.checkInpath(index1);
			// 如果全部选完
			details.checkSelectShop();
		}
	},
})
defineExpose({
	details
})
</script>
<style lang="scss" scoped>
.popupModel{
	background-color: #fff;
}
.purchase{
	width: 100vw;
	box-sizing: border-box;
	background-color: #ffffff;
	padding: 32rpx 0 8rpx;
	overflow: hidden;
	max-height: 100vh;
	border-radius: 24rpx 24rpx 0 0;
	box-sizing: border-box;
	position: relative;
	/* #ifndef MP-WEIXIN */
	padding: 32rpx 0 calc(8rpx + var(--safe-area-bottom));
	/* #endif */
}
.address{
	width: 100%;
	font-size: 24rpx;
	color: #7C807F;
	padding: 0 32rpx 0;
	box-sizing: border-box;
	border-bottom: 8rpx solid #F9F6F6;
	
	image{
		width: 40rpx;
		height: 40rpx;
	}
	.content_title{
		font-size: 30rpx;
		color: #1B2120;
		@include flex(center);
	}
	.content_peisong{
		margin: 24rpx 0;
		line-height: 42rpx;
	}
}
.shop{
	width: 100%;
	padding: 32rpx;
	box-sizing: border-box;
	border-bottom: 2rpx solid #F9F6F6;
	@include flex(flex-start,space-between);
	>image{
		width: 200rpx;
		height: 200rpx;
		border-radius: 24rpx;
	}
	>view{
		width: calc(100% - 224rpx);
		
	}
	.name{
		font-size: 32rpx;
		color: #1B2120;
	}
	.price{
		margin: 24rpx 0 32rpx;
		font-weight: bold;
		font-size: 30rpx;
		color: #FF6084;
	}
	.num{
		@include flex(center,space-between);
		.num_content{
			width: 220rpx;
			height: 80rpx;
			background: #FFFFFF;
			border-radius: 200rpx;
			border: 2rpx solid #E4E6E6;
			@include flex(center);
			.view{
				height: 80rpx;
				font-size: 32rpx;
				color: #1B2120;
				width: 92rpx;
				text-align: center;
				@include flex(center,center);
				&:first-child{
					width: 64rpx;
					border-right: 2rpx solid #E4E6E6;
				}
				&:last-child{
					width: 64rpx;
					border-left: 2rpx solid #E4E6E6;
				}
			}
		}
		.xian{
			font-size: 22rpx;
			color: #7C807F;
			.cun{
				color: #1B2120;
			}
		}
	}
}
//选择规格
.detailsSelect {
	height: 330rpx;
	overflow-y: scroll;
	padding: 48rpx 32rpx;
	box-sizing: border-box;
	position: relative;
	.scroll-Y {
		height: 300rpx;
	}

	.speSel_item {
		margin-bottom: 64rpx;

		&:last-child {
			margin-bottom: 0;
		}

		.speSel_item_title {
			font-weight: bold;
			font-size: 32rpx;
			color: #1B2120;
			margin-bottom: 24rpx;
		}

		.speSel_item_view {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
            
			view {
				padding: 32rpx 40rpx;
				font-size: 30rpx;
				margin: 0 32rpx 32rpx 0;
				color: #808080;
				min-width: 160rpx;
				background: #ffffff;
				border-radius: 400rpx;
				border: 2rpx solid #CACCCC;
				position: relative;
                text-align: center;
				text {
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 24rpx;
					width: 56rpx;
					height: 32rpx;
					color: #ffffff;
					border-radius: 3px 3px 0px 3px;
					position: absolute;
					right: 0;
					top: -16rpx;
				}
			}

			// 缺货
			.outOfStock {
				color: #E4E6E6;
				border: 1px solid #E4E6E6;
				background: #F5F6F7;
				text {
					color: #ffffff;
					background: #E4E6E6;
				}
			}

			.outOfStockText {
				background: #E4E6E6;
			}

			//选中
			.active {
				color: #FF6084;
				border: 2rpx solid #FF6084;

				text {
					background: #FF6084;
					right: -2rpx;
				}
			}
		}
	}
}
.buttonTwo {
	width: 100%;
	// height: 88rpx;
	font-size: 30rpx;
	margin: 60rpx auto 0;
	padding:8rpx 32rpx;
	box-sizing: border-box;
	border-top: 2rpx solid #E6F6F6;
	@include flex(center,space-between);
	view {
		width: 330rpx;
		height: 88rpx;
		color: #fff;
		background: linear-gradient( 90deg, #FF6188 0%, #FF5252 100%);
		
		border-radius: 44rpx;
		@include flex(center,center);
		&:first-child {
			color: #FF6084;
			background: #fff;
			border: 1px solid #FF6084;
		}
	}
}
.button {
	width: 100%;
	font-size: 30rpx;
	margin: 60rpx auto 0;
	padding:8rpx 32rpx;
	box-sizing: border-box;
	border-top: 2rpx solid #E6F6F6;
	view{
		font-size: 28rpx;
		color: #FFFFFF;
		height: 88rpx;
		background: linear-gradient( 90deg, #FF6188 0%, #FF5252 100%);
		border-radius: 44rpx;
		@include flex(center,center);
	}
}
.colse{
	position: absolute;
	top: -70rpx;
	right: 16rpx;
	width: 48rpx;
	height: 48rpx;
	border-radius: 50%;
}
</style>